package com.itbaizhan.openhis.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itbaizhan.openhis.domain.Scheduling;
import com.itbaizhan.openhis.dto.SchedulingFormDto;
import com.itbaizhan.openhis.dto.SchedulingQueryDto;
import com.itbaizhan.openhis.mapper.SchedulingMapper;
import com.itbaizhan.openhis.service.SchedulingService;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

/**
 *
 */
//@Service
@Service
public class SchedulingServiceImpl extends ServiceImpl<SchedulingMapper, Scheduling>
    implements SchedulingService{

    @Autowired
    private SchedulingMapper schedulingMapper;

    //查询排班数据
    @Override
    public List<Scheduling> queryScheduling(SchedulingQueryDto schedulingQueryDto) {
        QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
        wrapper.eq(schedulingQueryDto.getUserId() != null,Scheduling.COL_USER_ID,schedulingQueryDto.getUserId());
        wrapper.eq(schedulingQueryDto.getDeptId() != null,Scheduling.COL_DEPT_ID,schedulingQueryDto.getDeptId());
        wrapper.ge(StringUtils.isNotBlank(schedulingQueryDto.getBeginDate()),Scheduling.COL_SCHEDULING_DAY,schedulingQueryDto.getBeginDate());
        wrapper.le(StringUtils.isNotBlank(schedulingQueryDto.getEndDate()),Scheduling.COL_SCHEDULING_DAY,schedulingQueryDto.getEndDate());

        return schedulingMapper.selectList(wrapper);
    }

    @Override
    public int saveScheduling(SchedulingFormDto schedulingFormDto) {
        //判断前端传来的数据是否为空
        if(schedulingFormDto.getData() != null && schedulingFormDto.getData().size() > 0){
            DateTime dateTime =DateUtil.parse(schedulingFormDto.getBeginDate(),"yyyy-MM-dd");
            //得到的dataTime所在的周的周一
            DateTime beginData = DateUtil.beginOfWeek(dateTime);
            DateTime endTime = DateUtil.endOfWeek(dateTime);
            //转换格式，得到周一与周日的日期
            String start = DateUtil.format(beginData,"yyyy-MM-dd");
            String end = DateUtil.format(endTime,"yyyy-MM-dd");
            //获取用户名与部门
            SchedulingFormDto.SchedulingData schedulingData = schedulingFormDto.getData().get(0);
            Long userId = schedulingData.getUserId();
            Long deptId = schedulingData.getDeptId();
            if(userId != null){
                //先删除，再保存，因为可能前端多按几次保存键，造成多次提交
                QueryWrapper<Scheduling> wrapper = new QueryWrapper<>();
                wrapper.eq(Scheduling.COL_USER_ID,userId);
                wrapper.eq(Scheduling.COL_DEPT_ID,deptId);
                wrapper.between(Scheduling.COL_SCHEDULING_DAY,start,end);
                schedulingMapper.delete(wrapper);
                //保存
                List<String> schedulingDays = initScheduling(beginData);
                //数据保存在SchedulingData中，是以list形式保存
                for(SchedulingFormDto.SchedulingData datum : schedulingFormDto.getData()){
                    Scheduling scheduling = null;
                    int i = 0;
                    //遍历 上  下  晚
                    for (String s : datum.getSchedulingType()) {
                       if(StringUtils.isNotBlank(s)) {
                           scheduling = new Scheduling(userId, deptId, schedulingDays.get(i), datum.getSubsectionType(),
                                   s, DateUtil.date(), schedulingFormDto.getSimpleUser().getUserName());
                           schedulingMapper.insert(scheduling);
                       }
                        i++;
                    }

                }
                return 1;
            }else {
                return 0;
            }

        }
        return 0;
    }

    /**
     * 根据条件查询部门id
     * @param deptId
     * @param schedulingDay
     * @param schedulingType
     * @param subsectionType
     * @return
     */
    @Override
    public List<Long> queryHasSchedulingDeptIds(Long deptId, String schedulingDay, String schedulingType, String subsectionType) {
        return schedulingMapper.queryHasSchedulingDeptIds(deptId,schedulingDay,schedulingType,subsectionType);
    }

    //初始化一周的时间到list中
    private List<String> initScheduling(DateTime start) {
        List<String> list = new ArrayList<>();
        for (int i = 0; i <7 ; i++) {
            DateTime dateTime = DateUtil.offsetDay(start,i);
            list.add(DateUtil.format(dateTime,"yyyy-MM-dd"));

        }
        return list;
    }
}




